//
//  AppDelegate.swift
//  你的AI
//
//  Created by 丸子 on 2024/5/28.
//

import UIKit
import IQKeyboardManagerSwift
//import ATAuthSDK

let freeExecutionKey = "freeExecutionCount"
let first = "firststart"

let maxFreeExecutions = 3

@main
 class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // 检查是否是第一次运行，如果是则初始化计数
//        if UserDefaults.standard.integer(forKey: freeExecutionKey) == 0 {
//            UserDefaults.standard.set(0, forKey: freeExecutionKey)
//        }
        
        SqlManager.shared.create(AccountModel.self)
        SqlManager.shared.create(AIMessage.self)
        SqlManager.shared.create(PaymentTransactionInfo.self)
        if let accout = SqlManager.shared.filter(from: AccountModel.self)?.first {
            Appearance.shared.accoutModel = accout
        }
//        XLog.d("", TXCommonHandler.sharedInstance().getVersion())
//        TXCommonHandler.sharedInstance()
        loadWindow()
        return true
    }
    
    func loadWindow(){
        if let a = UserDefaults.standard.string(forKey: first) {
            if a == "1" {
                let screen = UIScreen.main.bounds
                window = UIWindow(frame: screen)
                self.window?.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
                window?.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT)
                self.window?.rootViewController = AITabbarViewController()
                self.window?.makeKeyAndVisible()
                IQKeyboardManager.shared.enable = true
//                IQKeyboardManager.shared.enableAutoToolbar = false
//                IQKeyboardManager.shared.shouldShowToolbarPlaceholder = false
                IQKeyboardManager.shared.keyboardDistanceFromTextField = -(kMusicBarHeight - 13)
            }else {
                let vc = FirststartViewController()
                let screen = UIScreen.main.bounds
                window = UIWindow(frame: screen)
                self.window?.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
                window?.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT)
                self.window?.rootViewController = vc
                self.window?.makeKeyAndVisible()
            }
        }else {
            let vc = FirststartViewController()
            let screen = UIScreen.main.bounds
            window = UIWindow(frame: screen)
            self.window?.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
            window?.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT)
            self.window?.rootViewController = vc
            self.window?.makeKeyAndVisible()
        }
    }
    
    
}
